package com.slz3.business.module.afterSaleManagement.aftApplyOrderLine.service;

import com.slz3.business.frame.enums.ValidFlag;
import com.slz3.business.frame.module.Repositorys;
import com.slz3.business.frame.module.Services;
import com.slz3.business.frame.steward.PageableSteward;
import com.slz3.business.module.afterSaleManagement.aftApplyOrderLine.domain.AftApplyOrderLine;
import com.slz3.business.module.afterSaleManagement.aftApplyOrderLine.domain.AftApplyOrderLineFactory;
import com.slz3.business.module.afterSaleManagement.aftApplyOrderLine.steward.AftApplyOrderLineSpecification;
import lombok.extern.slf4j.Slf4j;
import org.springframework.data.domain.Page;
import org.springframework.stereotype.Service;
import org.springframework.util.MultiValueMap;

import java.util.*;

@Slf4j
@Service
public class AftApplyOrderLineServiceImpl implements AftApplyOrderLineService {
    @Override
    public AftApplyOrderLine save(AftApplyOrderLine aftApplyOrderLine) {
        return Repositorys.aftApplyOrderLine.save(AftApplyOrderLineFactory.initBaseFields(aftApplyOrderLine));
    }
    @Override
    public Page<AftApplyOrderLine> page(MultiValueMap<String, String> params) {
        Page<AftApplyOrderLine> aftApplyOrderLinePage = Repositorys.aftApplyOrderLine.findAll(
                AftApplyOrderLineSpecification.findJoin(params), PageableSteward.extract(params)
        );
        return aftApplyOrderLinePage;
    }


    @Override
    public AftApplyOrderLine modify(AftApplyOrderLine aftApplyOrderLineNew) {
        return Repositorys.aftApplyOrderLine.saveAndFlush(AftApplyOrderLineFactory.initBaseFields(aftApplyOrderLineNew));
    }

    @Override
    public Optional<AftApplyOrderLine> single(UUID uuid) {
        return Repositorys.aftApplyOrderLine.findById(uuid);
    }

    @Override
    public List<AftApplyOrderLine> findAllByUuids(Iterable<UUID> uuids) {
        return Repositorys.aftApplyOrderLine.findAllById(uuids);
    }

    @Override
    public List<AftApplyOrderLine> delete(List<AftApplyOrderLine> aftApplyOrderLineList) {
        aftApplyOrderLineList.stream().forEach(item -> {
            item.setValidFlag(ValidFlag.DEL);
            AftApplyOrderLineFactory.initBaseFields(item);
        });
        return Services.aftApplyOrderLineTransactional.batchModify(aftApplyOrderLineList);
    }

    @Override
    public List<AftApplyOrderLine> findBySerAfterSaleId(UUID serAfterSaleId) {
        return Repositorys.aftApplyOrderLine.findBySerAfterSaleId(serAfterSaleId);
    }
}
